package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import  entity.ProduceExample;
import  service.inter.IProduceExampleService;
import  tool.JsonUtil;

@Controller
@RequestMapping("produceExample")
public class ProduceExampleController {
	@Autowired
	private IProduceExampleService iProduceExampleService;
	@RequestMapping("/add")
	public void add(ProduceExample produceExample,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
       Map map= iProduceExampleService.add(produceExample, request, response);
      String json= JsonUtil.getJson(map);
      json=json.replaceAll("\\\\\\\"", "\"");
      	try {
			String callback=(String)request.getAttribute("callback");
			 PrintWriter out = response.getWriter(); 
			 if(callback!=null){
				 out.print(callback+"("+json+")");
			 }else{
				 out.print(json);
			 }
			 out.flush();  
		     out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@RequestMapping("/delete")
	public void deleteProduce(String rowids,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
       Map map=iProduceExampleService.delete(rowids, request, response);
      String json= JsonUtil.getJson(map);
      json=json.replaceAll("\\\\\\\"", "\"");
      	try {
			String callback=(String)request.getAttribute("callback");
			 PrintWriter out = response.getWriter(); 
			 if(callback!=null){
				 out.print(callback+"("+json+")");
			 }else{
				 out.print(json);
			 }
			 out.flush();  
		     out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@RequestMapping("/update")
	public void updateProduce(String produceExample,String produceStepExample,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
       Map map= iProduceExampleService.update(produceExample,produceStepExample, request, response);
      String json= JsonUtil.getJson(map);
      json=json.replaceAll("\\\\\\\"", "\"");
      	try {
			String callback=(String)request.getAttribute("callback");
			 PrintWriter out = response.getWriter(); 
			 if(callback!=null){
				 out.print(callback+"("+json+")");
			 }else{
				 out.print(json);
			 }
			 out.flush();  
		     out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@RequestMapping("{indexnum}/{batchnum}")
	public void getProduceExample(HttpServletResponse response,HttpServletRequest request,
			@PathVariable String indexnum,@PathVariable String batchnum){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
       Map map= iProduceExampleService.queryByBatchnum(indexnum,batchnum);
      String json= JsonUtil.getJson(map);
      json=json.replaceAll("\\\\\\\"", "\"");
      	try {
			String callback=(String)request.getAttribute("callback");
			 PrintWriter out = response.getWriter(); 
			 if(callback!=null){
				 out.print(callback+"("+json+")");
			 }else{
				 out.print(json);
			 }
			 out.flush();  
		     out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping("getProduceExampleList")
	public void getProductList(HttpServletResponse response,HttpServletRequest request,
			String start,String limit){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
       Map map= iProduceExampleService.query(start, limit);
      String json= JsonUtil.getJson(map);
      json=json.replaceAll("\\\\\\\"", "\"");
      	try {
			String callback=(String)request.getAttribute("callback");
			 PrintWriter out = response.getWriter(); 
			 if(callback!=null){
				 out.print(callback+"("+json+")");
			 }else{
				 out.print(json);
			 }
			 out.flush();  
		     out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@RequestMapping("getProduceExample/{rowid}")
	public void getProduceExample(HttpServletResponse response,HttpServletRequest request,
			String rowid){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
       Map map= iProduceExampleService.query(rowid);
      String json= JsonUtil.getJson(map);
      json=json.replaceAll("\\\\\\\"", "\"");
      	try {
			String callback=(String)request.getAttribute("callback");
			 PrintWriter out = response.getWriter(); 
			 if(callback!=null){
				 out.print(callback+"("+json+")");
			 }else{
				 out.print(json);
			 }
			 out.flush();  
		     out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
